package com.example.maximum;

/**
 * 入参是一个int数组，例如[4,6,2,1,7,6] 代表商品的价格走势，先买后卖，最多买卖一次，求如何得到商品的最大收益
 */
public class MaximumReturn {

    public static void main(String[] args) {
        int[] prices = new int[]{4, 6, 2, 1, 7, 6};
        int temp = 0;
        int start = 0;
        int end = 0;
        int val = 0;
        for (int i = 0; i < prices.length; i++) {
            if (prices[temp] > prices[i]) {
                temp = i;
            } else if (val < prices[i] - prices[temp]) {
                val = prices[i] - prices[temp];
                start = temp;
                end = i;
            }
        }
        System.out.println("start=" + start);
        System.out.println("end=" + end);
        System.out.println("val=" + val);

    }
}
